#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QJsonDocument>
#include <QFile>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    manager = new QNetworkAccessManager(this);
    connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(slotFinished(QNetworkReply*)));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::slotFinished(QNetworkReply *reply)
{
    QByteArray bytes = reply->readAll();
    QJsonDocument jsonDoc = QJsonDocument::fromJson(bytes);
    QByteArray jsonBytes = jsonDoc.toJson();
    ui->textBrowser->setText(QString::fromUtf8(jsonBytes));
    QString fileName = "./xxx.json";
    QFile file(fileName);
    file.open(QFile::WriteOnly | QFile::Truncate);
    file.write(jsonBytes);
    file.close();
}

void MainWindow::on_pushButton_clicked()
{
   QNetworkRequest request;
   request.setRawHeader(QByteArray("Content-Type"), QByteArray("application/x-www-form-urlencoded; charset=UTF-8"));
   request.setUrl(QUrl("https://www.kuaidi100.com/company.do"));
   QString dataString = QString("method=companyjs&number=%1").arg(ui->lineEdit_company->text());
   manager->post(request, dataString.toUtf8());
}

void MainWindow::on_pushButton_2_clicked()
{
    QNetworkRequest request;
    QString url = "https://www.kuaidi100.com/query?";
    QString dataStr = QString("type=%1&postid=%2&id=1&valicode=&temp=0.827341845634965&phone=")
            .arg(ui->lineEdit_gongsi->text()).arg(ui->lineEdit_order->text());
    request.setUrl(QUrl(url + dataStr));
    manager->get(request);
}
